<?php

namespace Yousheng\Ku\Resolvers;

use Yousheng\Ku\Column;
use Yousheng\Ku\Contracts\ResolverInterface;
use LogicException;

/**
 * It is used by parent class of various resolver classes.
 */
abstract class Resolver implements ResolverInterface
{
    /**
     * Constructor.
     *
     * @param  \Yousheng\Ku\Column  $column  The column.
     */
    public function __construct(
        protected Column $column
    ) {}

    /**
     * {@inheritDoc}
     *
     * @internal This method should be implemented by child classes.
     *
     * @throw LogicException
     */
    abstract public function fake(): string;

    /**
     * {@inheritDoc}
     *
     * @internal This method should be implemented by child classes.
     *
     * @throw LogicException
     */
    abstract public function migration(): string;

    /**
     * {@inheritDoc}
     *
     * @internal This method should be implemented by nova resource classes.
     *
     * @throw LogicException
     */
    abstract public function nova(): ?string;

    /**
     * Finally process for resolvers
     *
     * @param  string  $migration  The migration payload before post processing
     * @return string The method returns the migration payload after post processing
     */
    public function last(string $migration): string
    {
        if (! $this->column->notNull) {
            $migration .= '->nullable()';
        }
        if (is_null($this->column->default)) {
            $default = null;
        } else {
            $intType = ["tinyint", "smallint", "mediumint", "int", "bigint"];
            $floatType = ["float", "double", "numeric", "decimal"];
            $type = strtolower($this->column->type);
            if (in_array($type, $intType)) {
                $default = (int)$this->column->default;
            } elseif (in_array($type, $floatType)) {
                $default = (float)$this->column->default;
            } else {
                if (!is_string($this->column->default)) {
                    echo "{$this->column->tableName}中列{$this->column->field}的默认值不是字符串类型。\n";
                    dump($this->column);
                } else {
                    $default = "'{$this->column->default}'"; // 这里{}不能少，否则先打印$this->column这个对象，调用 __toString方法
                }
            }
        }
        if (/* $this->column->notNull &&  */$default === null) {
            $migration .= '';
        } else {
            $migration .= "->default($default)";
        }




        // 改在Index中处理主键

        // if ($this->column->primaryKey === true && $this->column->field !== 'id') {
        //     $migration .= '->primary()';
        // }

        return $migration . ';';
    }

    /**
     * Get the attribute casting
     *
     * @return string|null The method returns the attribute casting
     */
    public function cast(): ?string
    {
        return null;
    }

    /**
     * Reading data from inaccessible (protected or private) or non-existing properties.
     *
     * @param  string  $property  The property name.
     * @return mixed The value of the `column` property.
     */
    public function __get(string $property): mixed
    {
        return $this->column->$property;
    }
}
